package com.example.jee_springboot.mapper;

import com.example.jee_springboot.entity.Stu;
import org.apache.ibatis.annotations.*;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
@Repository
@Mapper
public interface StuMapper {
    @Cacheable("all::stus")//把结果缓存到all::stus 里
    //1.引入包     implementation group: 'org.springframework.boot', name: 'spring-boot-starter-cache'
    // 2.JeeSpringbootApplication 类上方加@EnableCaching
    //3. 在这个位置的类里加入 缓存到哪里 @Cacheable("all::stus")//把结果缓存到all::stus 里
    //4. 测试CacheTest
    @Select("select * from stu")
    List<Stu> findAll();


    @Select("select * from stu")
    List<Map> findAll2();//列名和对应的值


    @Select("select * from stu where id=#{id}")
    Stu findById(long id);


    @Insert("insert into stu(username) values(#{username})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    int add(Stu stu);//执行sql语句影响了多少行


    @CacheEvict(value="all::stus*",allEntries = true)  //清除缓存
    @Insert("insert into stu(username) values(#{stu.username})")
    @Options(useGeneratedKeys = true,keyProperty = "stu.id",keyColumn = "id") //ketColumn自动产生列的列名，keyProperty：接受值的变量名
    int add2(@Param("stu") Stu stu);


    //得到产生的id
    @Select("select * from stu where username=#{username} and pass=#{pass}")
    Stu checkLogin(String username,String pass);


    @Select("select count(*) from stu where username=#{username} and pass=#{pass}")
    int checkLogin2(String username,String pass);
//
//    @Select("select id,username from stu")
//    List<StuRecord> findAllByRecord();


}